const orderModel = require("../model/orderModel");

module.exports.findAll = async function(searchObj,page){
    // 分页查询
    let {current,size} = page;
    // 获取数据总数
    let total = await orderModel.countDocuments(searchObj)
    // 根据总数/每页显示数得到总页数
    let totalPage = Math.ceil(total / size);
    // limit是每页显示数，skip通过(当前页-1) * 每页显示得到
    let rows = await orderModel
                .find(searchObj)
                .populate({path:'users'})
                .limit(parseInt(size))
                .skip((current - 1) * size);
    return {
        current,size,total,totalPage,rows
    }
}
// 查所有
module.exports.findAllOrder =async function(){
    return await orderModel.find()
}
module.exports.insert = async function(order){
    return await orderModel.create(order);
}

module.exports.update = async function(id,order){
    return await orderModel.updateOne({_id:id},order);
}

module.exports.delete = async function(id){
    return await orderModel.deleteOne({_id:id});
}

module.exports.findById = async function (id) {
    return await orderModel.findById(id);
}